package com.supercube.sport.yoga.service.impl;

import com.supercube.common.utils.StringUtils;
import com.supercube.sport.yoga.dao.SalaryDesignMapper;
import com.supercube.sport.yoga.dao.SalaryGradientMapper;
import com.supercube.sport.yoga.domain.SalaryDesignDTO;
import com.supercube.sport.yoga.domain.SalaryGradientDTO;
import com.supercube.sport.yoga.service.SalaryDesignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class SalaryDesignServiceImpl implements SalaryDesignService {

    // 薪酬设置
    @Autowired
    private SalaryDesignMapper salaryDesignMapper;

    // 梯度梯度
    @Autowired
    private SalaryGradientMapper salaryGradientMapper;

    @Override
    public int save(SalaryDesignDTO dto) {
        String salaryType = dto.getSalaryType(); // 1：固定工资；2：提成；3：课时费
        String id = StringUtils.getUUID();
        dto.setId(id);
        int insert = 0;
        if ("1".equals(salaryType)) {
            insert = salaryDesignMapper.insert(dto);
        } else if (Arrays.asList("2", "3").contains(salaryType)) {
            insert = salaryDesignMapper.insert(dto);
            List<SalaryGradientDTO> salaryGradientDTOList = dto.getSalaryGradientDTOList();
            salaryGradientDTOList.forEach(salaryGradientDTO -> {
                salaryGradientDTO.setId(StringUtils.getUUID());
                salaryGradientDTO.setSalaryDesignId(id);
            });
            salaryGradientMapper.batchInsert(salaryGradientDTOList);
        }
        return insert;
    }

    @Override
    public int update(SalaryDesignDTO dto) {
        salaryDesignMapper.updateByPrimaryKey(dto);
        salaryGradientMapper.deleteBySalaryDesignId(dto.getId());
        List<SalaryGradientDTO> salaryGradientDTOList = dto.getSalaryGradientDTOList();
        salaryGradientDTOList.forEach(salaryGradientDTO -> {
            salaryGradientDTO.setId(StringUtils.getUUID());
            salaryGradientDTO.setSalaryDesignId(dto.getId());
        });
        salaryGradientMapper.batchInsert(salaryGradientDTOList);
        return 0;
    }

    @Override
    public int delete(String id) {
        salaryGradientMapper.deleteBySalaryDesignId(id);
        int delete = salaryDesignMapper.deleteByPrimaryKey(id);
        return delete;
    }

    @Override
    public SalaryDesignDTO querySalaryDesignById(String id) {
        SalaryDesignDTO salaryDesignDTO = salaryDesignMapper.selectByPrimaryKey(id);
        List<SalaryGradientDTO> salaryGradientDTOS = salaryGradientMapper.selectBySalaryDesignId(id);
        salaryDesignDTO.setSalaryGradientDTOList(salaryGradientDTOS);
        return salaryDesignDTO;
    }

    @Override
    public SalaryDesignDTO querySalaryDesignByName(String name) {

        return salaryDesignMapper.selectByName(name);
    }

    @Override
    public List<SalaryDesignDTO> querySalaryDesignList(String salaryType) {
        List<SalaryDesignDTO> salaryDesignDTOS = salaryDesignMapper.selectSalaryDesignList(salaryType);
        salaryDesignDTOS.forEach(salaryDesignDTO ->
                salaryDesignDTO.setSalaryGradientDTOList(salaryGradientMapper.selectBySalaryDesignId(salaryDesignDTO.getId())));
        return salaryDesignDTOS;
    }

}
